const wx2my = require('../../wx2my');

// pages/user_deposit/user_deposit.js
const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    paySus: true,
    container: true,
    noDeposit: true,
    apply: 'apply'
  },
  onLoad: function (e) {},
  onReady: function () {},
  onShow: function () {
    page = 1;
    this.get_list();
    this.get_phone();
  },

  get_phone() {
    let that = this;
    app.wxRequest('customer', {}, function (res) {
      that.setData({
        phone: res.data.data.tel
      });
    });
  },

  get_list() {
    let that = this;
    app.wxRequest('mydeposit', {
      'token': wx2my.getStorageSync('token'),
      'page': page
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let totalDeposit = res.data.data.total_money;

        if (totalDeposit <= 0) {
          that.setData({
            noDeposit: false
          });
        } else {
          that.setData({
            container: false
          });
        }

        let list = res.data.data.list.list;

        if (page == 1) {
          totalpage = res.data.data.list.totalpage;
        }

        that.setData({
          totalDeposit: totalDeposit,
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false
        });
      }
    });
  },

  apply(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('refund_deposit', {
      'token': wx2my.getStorageSync('token'),
      'deposit_id': id
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        page = 1;
        that.get_list();
        that.setData({
          paySus: false
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  del() {
    this.setData({
      paySus: true
    });
  },

  underwayOrder() {
    this.setData({
      paySus: true
    });
  },

  toCall() {
    wx2my.makePhoneCall({
      phoneNumber: this.data.phone
    });
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {
    page++;

    if (!this.data.lock) {
      this.get_list();
    }
  },
  onShareAppMessage: function () {}
});